package com.tbit.api;

import com.alibaba.fastjson.JSON;
import com.tbit.common.entity.view.Result;
import com.tbit.manager.api.OrderOAApi;
import com.tbit.manager.api.dto.OrderFunnelDTO;
import com.tbit.manager.api.dto.OrderStatReturnDTO;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.dto.OrderAnalysisQueryDTO;
import com.tbit.uqbike.service.business.OrderAnalysisService;
import com.tbit.uqbike.service.business.operationsanalysis.OrderOAService;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class OrderOAApiImpl implements OrderOAApi {

    @Autowired
    private OrderOAService orderOAService;
    @Resource
    private OrderAnalysisService orderAnalysisService;

    /**
     * @description: 获取车辆订单的统计数据（订单收益、订单量、车均收益等）
     * @author: mo.shanyong
     * @date: 2025/3/7 13:39
     * @param: accountId 区域Id
     * @param: startDate 筛选开始时间
     * @param: endDate 筛选结束时间
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> getOrderBaseData(Integer accountId, String startDate, String endDate) {

        return orderOAService.getOrderBaseData(accountId, DateTimeUtil.StringToDateTime(startDate), DateTimeUtil.StringToDateTime(endDate));
    }

    /**
     * @description: 获取订单收益折线图数据
     * @author: mo.shanyong
     * @date: 2025/3/7 13:43
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @param: endDate 结束时间
     * @return: java.util.List<com.tbit.manager.api.dto.OrderStatReturnDTO>
     */
    @Override
    public List<OrderStatReturnDTO> getOrderFeeLineData(Integer accountId,  String startDate, String endDate) {
        return orderOAService.getOrderFeeLineData(accountId, DateTimeUtil.StringToDateTime(startDate), DateTimeUtil.StringToDateTime(endDate));
    }
    /**
     * @description: 订单量折线图数据
     * @author: mo.shanyong
     * @date: 2025/3/7 13:51
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @param: endDate 结束时间
     * @return: java.util.List<com.tbit.manager.api.dto.OrderStatReturnDTO>
     */
    @Override
    public List<OrderStatReturnDTO> getOrderNumLineData(Integer accountId,  String startDate, String endDate) {
        return orderOAService.getOrderNumLineData(accountId, DateTimeUtil.StringToDateTime(startDate), DateTimeUtil.StringToDateTime(endDate));
    }

    /**
     * @description: 车均收益折线图数据
     * @author: mo.shanyong
     * @date: 2025/3/7 13:51
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @param: endDate 结束时间
     * @return: java.util.List<com.tbit.manager.api.dto.OrderStatReturnDTO>
     */
    @Override
    public List<OrderStatReturnDTO> getMachineAvgFeeLineData(Integer accountId,  String startDate, String endDate) {
        return orderOAService.getMachineAvgFeeLineData(accountId, DateTimeUtil.StringToDateTime(startDate), DateTimeUtil.StringToDateTime(endDate));
    }

    /**
     * @description: 车均单量折线图数据
     * @author: mo.shanyong
     * @date: 2025/3/7 14:56
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @param: endDate 结束时间
     * @return: java.util.List<com.tbit.manager.api.dto.OrderStatReturnDTO>
     */
    @Override
    public List<OrderStatReturnDTO> getMachineAvgOrderLineData(Integer accountId,  String startDate, String endDate) {
        return orderOAService.getMachineAvgOrderLineData(accountId, DateTimeUtil.StringToDateTime(startDate), DateTimeUtil.StringToDateTime(endDate));
    }
    /**
     * @description: 正常、站外、超区订单折线图数据
     * @author: mo.shanyong
     * @date: 2025/3/7 15:03
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @param: endDate 结束时间
     * @return: com.tbit.manager.api.dto.OrderStatReturnDTO
     */
    @Override
    public List<OrderStatReturnDTO> getOrderFinishTypeData(Integer accountId,  String startDate, String endDate) {
        return orderOAService.getOrderFinishTypeData(accountId, DateTimeUtil.StringToDateTime(startDate), DateTimeUtil.StringToDateTime(endDate));
    }
    /**
     * @description: 车辆平均里程饼图数据
     * @author: mo.shanyong
     * @date: 2025/3/7 15:06
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @param: endDate 结束时间
     * @return: java.util.List<com.tbit.manager.api.dto.OrderStatReturnDTO>
     */
    @Override
    public List<OrderStatReturnDTO> getOrderMileagePieData(Integer accountId,  String startDate, String endDate) {
        return orderOAService.getOrderMileagePieData(accountId, DateTimeUtil.StringToDateTime(startDate), DateTimeUtil.StringToDateTime(endDate));
    }
    /**
     * @description: 获取订单支付方式饼图数据
     * @author: mo.shanyong
     * @date: 2025/3/7 15:11
     * @param: accountId 区域id
     * @param: startDate 开始时间
     * @param: endDate 结束时间
     * @return: com.tbit.manager.api.dto.OrderStatReturnDTO
     */
    @Override
    public List<OrderStatReturnDTO> getOrderPayTypePieData(Integer accountId,  String startDate, String endDate) {
        return orderOAService.getOrderPayTypePieData(accountId, DateTimeUtil.StringToDateTime(startDate), DateTimeUtil.StringToDateTime(endDate));
    }
    /**
     * @description: 获取时间段内区域的订单的骑行时长的分布数据
     * @author: mo.shanyong
     * @date: 2025/3/14 9:11
     * @param: accountId 区域id
     * @param: startDate 开始时间 2025-03-01 00:00:00
     * @param: endDate 结束时间 2025-03-01 23:59:59
     * @return: java.util.List<com.tbit.manager.api.dto.OrderStatReturnDTO>
     */
    @Override
    public List<OrderStatReturnDTO> getOrderTimeDistribution(Integer accountId,  String startDate, String endDate) {
        return orderOAService.getOrderPayTypePieData(accountId, DateTimeUtil.StringToDateTime(startDate), DateTimeUtil.StringToDateTime(endDate));
    }

    /**
     * @description: 获取订单转换数据
     * @author: mo.shanyong
     * @date: 2025/4/17 11:21
     * @param: accountId 区域id
     * @param: startDate 筛选开始时间
     * @param: endDate  筛选结束时间
     * @return: com.tbit.manager.api.dto.OrderFunnelDTO
     */
    @Override
    public OrderFunnelDTO orderAnalysisFunnel(Integer accountId, String startDate, String endDate) {
        OrderAnalysisQueryDTO orderAnalysisQueryDTO = new OrderAnalysisQueryDTO();
        orderAnalysisQueryDTO.setAccountId(accountId);
        orderAnalysisQueryDTO.setStartTime(DateTimeUtil.StringToDateTime(startDate));
        orderAnalysisQueryDTO.setEndTime(DateTimeUtil.StringToDateTime(endDate));
        Result<Object> objectResult = orderAnalysisService.orderAnalysisFunnel(orderAnalysisQueryDTO);
        if (objectResult.unSuccess()){
            throw new BaseException(objectResult.getMsg());
        }
        return JSON.parseObject(JSON.toJSONString(objectResult.getData()), OrderFunnelDTO.class);
    }
}
